﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

using Microsoft.Practices.ServiceLocation;

using Happy.Web.Mvc.ExceptionHanding;
using Happy.Web.Mvc.Newtonsoft;

namespace Happy.Web.Mvc
{
    /// <summary>
    /// 面向Ajax的控制器。
    /// </summary>
    [WriteExceptionResult]
    public abstract class AjaxController : HappyController, ISupportDefaultModelBinder
    {
        /// <inheritdoc />
        public virtual IModelBinder DefaultModelBinder
        {
            get
            {
                return NewtonsoftJsonBinder.Instance;
            }
        }

        /// <summary>
        /// 输出JSON。
        /// </summary>
        protected NewtonsoftJsonResult NewtonsoftJson(object data)
        {
            return this.NewtonsoftJson(data, null, null);
        }

        /// <summary>
        /// 输出JSON。
        /// </summary>
        protected NewtonsoftJsonResult NewtonsoftJson(object data, string contentType)
        {
            return this.NewtonsoftJson(data, contentType, null);
        }

        /// <summary>
        /// 输出JSON。
        /// </summary>
        protected virtual NewtonsoftJsonResult NewtonsoftJson(object data, string contentType, Encoding contentEncoding)
        {
            return new NewtonsoftJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding
            };
        }
    }
}
